package io.renren.service.repair.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.renren.common.service.impl.CrudServiceImpl;

import io.renren.common.utils.ConvertUtils;
import io.renren.dao.repair.RepairInfoDao;
import io.renren.dao.repair.RepairTaskDao;
import io.renren.dto.repair.RepairTaskDTO;
import io.renren.entity.repair.RepairInfoEntity;
import io.renren.entity.repair.RepairTaskEntity;
import io.renren.service.repair.RepairTaskService;
import io.renren.vo.TaskRespVO;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * 维修任务表
 *
 * @author dio 167762802@QQ.com
 * @since 1.0.0 2025-04-12
 */
@Service
public class RepairTaskServiceImpl extends CrudServiceImpl<RepairTaskDao, RepairTaskEntity, RepairTaskDTO> implements RepairTaskService {
    @Resource
    private RepairInfoDao repairInfoDao;
    @Resource
    private RepairTaskDao repairTaskDao;

    @Override
    public QueryWrapper<RepairTaskEntity> getWrapper(Map<String, Object> params) {
        String id = (String) params.get("id");

        QueryWrapper<RepairTaskEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StrUtil.isNotBlank(id), "id", id);

        return wrapper;
    }


    @Override
    public List<TaskRespVO> getRepairTask() {

        List<RepairInfoEntity> repairInfoEntities = repairInfoDao.selectList(null);

        List<TaskRespVO> taskRespVOS = repairInfoEntities.stream().map(item -> {
            TaskRespVO taskRespVO = new TaskRespVO();
            BeanUtil.copyProperties(item, taskRespVO);
            return taskRespVO;
        }).toList();
        return taskRespVOS;



    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateById(Long id, Long userId) {

        RepairTaskEntity repairTaskEntity = repairTaskDao.selectByRepairId(id);
        repairTaskEntity.setStatus(1);
        repairTaskEntity.setAssignTime(Timestamp.valueOf(LocalDateTime.now()));
        repairTaskEntity.setStartTime(Timestamp.valueOf(LocalDateTime.now()));
        repairTaskEntity.setWorkerId(userId);
        repairTaskDao.updateById(repairTaskEntity);

        RepairInfoEntity repairInfoEntity = repairInfoDao.selectById(repairTaskEntity.getRepairId());
        repairInfoEntity.setStatus(1);
        repairInfoDao.updateById(repairInfoEntity);


    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateByIdFinish(Long id) {
        RepairTaskEntity repairTaskEntity = repairTaskDao.selectByRepairId(id);
        repairTaskEntity.setFinishTime(Timestamp.valueOf(LocalDateTime.now()));
        repairTaskEntity.setStatus(2);
        repairTaskDao.updateById(repairTaskEntity);

        RepairInfoEntity repairInfoEntity = repairInfoDao.selectById(repairTaskEntity.getRepairId());
        repairInfoEntity.setStatus(2);
        repairInfoDao.updateById(repairInfoEntity);
    }

}